package edu.cuit.svsp.controller;

import edu.cuit.svsp.entity.Review;
import edu.cuit.svsp.request.ResponseResult;
import edu.cuit.svsp.service.ReviewService;
import edu.cuit.svsp.util.JWTUtil;
import edu.cuit.svsp.util.OssUtil;
import edu.cuit.svsp.vo.ReviewAddVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @className: ReviewController
 * @author: pjy
 * @description: 评论控制器
 * @date: 2024/7/6
 */
@RestController
@RequestMapping("/review")
@Slf4j
public class ReviewController {
    @Resource
    private OssUtil ossUtil;
    @Resource
    private ReviewService reviewService;

    // 上传图片
    @CrossOrigin
    @PostMapping("/imageUpload")
    public ResponseResult imageUpload(MultipartFile file) throws IOException {
        String url = ossUtil.upOss(file);
        return new ResponseResult(HttpStatus.OK.value(), "success",url);
    }

    // 添加评论
    @PostMapping("/add")
    public ResponseResult add(@RequestBody ReviewAddVo vo, HttpServletRequest request) {
        int uid = JWTUtil.getuid(request.getHeader("Authorization"));
        vo.setUid(uid);
        return new ResponseResult(HttpStatus.OK.value(), "success",reviewService.add(vo));
    }

    // 根据视频id查询评论
    @GetMapping("/findByVid/{vid}")
    public ResponseResult findByVid(@PathVariable("vid") Integer vid) {
        List<Review> list = reviewService.findByVid(vid);
//        log.debug(list.toString());
        return new ResponseResult(HttpStatus.OK.value(), "success",list);
    }

    // 删除评论
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        return new ResponseResult(HttpStatus.OK.value(), "success",reviewService.delete(id));
    }

    // 点赞
    @GetMapping("/updateUp/{up}/{id}")
    public ResponseResult updateUp(@PathVariable("up") boolean up, @PathVariable("id") Integer id) {
        return new ResponseResult(HttpStatus.OK.value(), "success",reviewService.updateUp(up,id));
    }

    // 评论列表
    @GetMapping("/list/{page}/{size}")
    public ResponseResult list(@PathVariable int page, @PathVariable int size){
        return new ResponseResult(HttpStatus.OK.value(), "success", reviewService.list(page,size)) ;
    }

    // 审核
    @PostMapping("/pass/{id}")
    public ResponseResult pass(@PathVariable("id") Integer id) {
        return new ResponseResult(HttpStatus.OK.value(), "success",reviewService.pass(id));
    }

    // 评论审核不通过
    @DeleteMapping("/out/{id}")
    public ResponseResult out(@PathVariable("id") Integer id) {
        return new ResponseResult(HttpStatus.OK.value(), "success",reviewService.out(id));
    }
}
